D:\a\tools.proto\tools.proto\compiler\src\model\typedef.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::model::message::{MessageField, MessageFieldValue}; |
30 | | use crate::model::protocol::Description; |
31 | | use crate::model::structure::{Offset, SimpleType, StructField, StructFieldRaw, StructFieldView}; |
32 | | use serde::Deserialize; |
33 | | |
34 | | #[derive(Clone, Debug, Deserialize)] |
35 | | pub struct Typedef { |
36 | | pub name: String, |
37 | | pub header: Option<String>, |
38 | | pub value: Option<MessageFieldValue>, |
39 | | pub optional: Option<bool>, |
40 | | pub description: Option<Description>, |
41 | | pub item_type: Option<String>, |
42 | | pub codec: Option<String>, |
43 | | pub raw: Option<StructFieldRaw>, |
44 | | pub view: Option<StructFieldView>, |
45 | | pub array_len: Option<usize>, |
46 | | pub offset: Option<Offset>, |
47 | | } |
48 | | |
49 | | impl Typedef { |
50 | 0 | pub fn to_struct(&self) -> Option<StructField> { |
51 | 0 | if self.raw.is_some() { Branch (51:12): [True: 0, False: 0]
Branch (51:12): [Folded - Ignored]
|
52 | 0 | Some(StructField { |
53 | 0 | name: self.name.clone(), |
54 | 0 | raw: self.raw.clone(), |
55 | 0 | view: self.view.clone(), |
56 | 0 | offset: self.offset.clone(), |
57 | 0 | array_len: self.array_len, |
58 | 0 | description: self.description.clone(), |
59 | 0 | item_type: self.item_type.clone(), |
60 | 0 | }) |
61 | | } else { |
62 | 0 | None |
63 | | } |
64 | 0 | } |
65 | | |
66 | 8 | pub fn to_message(&self) -> Option<MessageField> { |
67 | 8 | match &self.raw { |
68 | 8 | None => Some(MessageField { |
69 | 8 | name: self.name.clone(), |
70 | 8 | header: self.header.clone(), |
71 | 8 | value: self.value.clone(), |
72 | 8 | codec: self.codec.clone(), |
73 | 8 | description: self.description.clone(), |
74 | 8 | optional: self.optional, |
75 | 8 | item_type: self.item_type.clone(), |
76 | 8 | }), |
77 | 0 | Some(v) => { |
78 | 0 | let bit_size = v.get_bit_size(); |
79 | 0 | if v.get_simple_type() == SimpleType::Unsigned && bit_size % 8 == 0 { Branch (79:20): [True: 0, False: 0]
Branch (79:67): [True: 0, False: 0]
Branch (79:20): [Folded - Ignored]
Branch (79:67): [Folded - Ignored]
|
80 | 0 | Some(MessageField { |
81 | 0 | name: self.name.clone(), |
82 | 0 | header: self.header.clone(), |
83 | 0 | description: self.description.clone(), |
84 | 0 | codec: self.codec.clone(), |
85 | 0 | optional: self.optional, |
86 | 0 | item_type: self.item_type.clone(), |
87 | 0 | value: Some(MessageFieldValue::Unsigned { bits: bit_size }), |
88 | 0 | }) |
89 | | } else { |
90 | 0 | None |
91 | | } |
92 | | } |
93 | | } |
94 | 8 | } |
95 | | } |